package com.hp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hp.dao.GoodDao;
import com.hp.dao.TopDao;
import com.hp.dao.TypeDao;
import com.hp.entity.Good;
import com.hp.entity.Top;
import com.hp.entity.Type;
import com.hp.service.IGoodService;
import com.hp.vo.GoodVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiuQiang
 * @Date: 2021/10/27 11:53
 */
@Service
public class GoodServiceImpl implements IGoodService {

    @Autowired
    private GoodDao goodDao;
    @Autowired
    private TypeDao typeDao;
    @Autowired
    private TopDao topDao;

    @Override
    public PageInfo<GoodVo> findGoodAll(Integer page,Integer type) {
        //如果type!=null,就是点击了查看今日推荐，则根据id查询信息，copyBean返回
        if (0 != type){
            PageHelper.startPage(page,4);
            List<Good> goodList = goodDao.findTopsAllGoodId(type);
            PageInfo pageInfo = new PageInfo(goodList);
            List<GoodVo> goodVoList = copyBean(goodList);
            pageInfo.setList(goodVoList);
            return pageInfo;
        }else {
            //如果tyoe==null，就是每点击查看今日推荐，返回如下结果
            //查询出goods全部信息进行赋值
            PageHelper.startPage(page, 4);
            List<Good> goodList = goodDao.findGoodAll();
            PageInfo pageInfo = new PageInfo(goodList);
            List<GoodVo> goodVoList = copyBean(goodList);
            pageInfo.setList(goodVoList);
            return pageInfo;
        }
    }

    private List<GoodVo> copyBean(List<Good> goodList){
        List<GoodVo> goodVoList = new ArrayList<>();
        for (Good good : goodList){
            GoodVo goodVo = new GoodVo();
            //将属性值复制
            BeanUtils.copyProperties(good,goodVo);
            //根据goods的type_id查询出types表的name
            Type type = typeDao.findTypeById(good.getTypeId());
            //赋值
            goodVo.setTypeName(type.getName());
            //根据type的id查询tops的good_id
            Top top = topDao.findTopsById(good.getId());
            //根据good_id和goods表的id相对比，如果相同则top是true
            if (null != top){
                goodVo.setTop(true);
            }else {
                goodVo.setTop(false);
            }
            //添加进list
            goodVoList.add(goodVo);
        }
        return goodVoList;
    }

    @Override
    public int goodDelete(Integer goodId) {
        //查询tops表信息是不是今日推荐，也就是有没有这个信息
        Top top = topDao.findTopsById(goodId);
        //判断查询的返回值是不是为null，如果不是null，则需要删除tops表和goods表的信息
        if (null != top){
            topDao.topsDeleteById(goodId);
            return goodDao.goodDeleteById(goodId);
        }else {
            //如果是则没有信息，只需删除goods表信息
            return goodDao.goodDeleteById(goodId);
        }
    }

    @Override
    public List<Good> findTopsAllGoodId(Integer type) {
        return goodDao.findTopsAllGoodId(type);
    }

    @Override
    public int topDelete(Integer goodId) {
        return topDao.topsDeleteById(goodId);
    }

    @Override
    public int topSave(Integer goodId) {
        return topDao.saveTops(goodId);
    }

    @Override
    public int goodSave(Good good) {
        return goodDao.goodSave(good);
    }

    @Override
    public Good findGoodById(Integer id) {
        return goodDao.findGoodById(id);
    }

    @Override
    public int goodUpdate(Good good) {
        return goodDao.goodUpdate(good);
    }

    @Override
    public List<Good> findTopsAll() {
        return goodDao.findTopsAll();
    }

    @Override
    public List<Good> findGoodsBySales() {
        return goodDao.findGoodsBySales();
    }

    @Override
    public List<Good> findGoodAllByTypeId(Integer id) {
        return goodDao.findGoodAllByTypeId(id);
    }

    @Override
    public List<Good> findGoodsBySalesAll() {
        return goodDao.findGoodsBySalesAll();
    }

    @Override
    public List<Good> findGoodsBySalesNew() {
        return goodDao.findGoodsBySalesNew();
    }

    @Override
    public List<Good> findGoodsByNameLike(String name) {
        return goodDao.findGoodsByNameLike(name);
    }

    @Override
    public List<Good> findTopsAllToday() {
        return goodDao.findTopsAllToday();
    }

    @Override
    public List<Good> findGoodsByTypeId(Integer typeId) {
        return goodDao.findGoodsByTypeId(typeId);
    }

    @Override
    public Good findGoodsByGoodId(Integer goodId) {
        return goodDao.findGoodsByGoodId(goodId);
    }
}
